﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
using AccelerateSensor.Model.Node;

namespace AccelerateSensor.Model.Tools
{
    internal static class TypeExtension
    {
        private static readonly NameValueCollection NodeTypeNvc = GetEnumDescription(typeof(NodeType));

        public static string GetDescription(this NodeType type)
        {
            return NodeTypeNvc.Get(type.ToString());
        }

        /// <summary>
        /// 获取枚举描述信息
        /// </summary>
        public static NameValueCollection GetEnumDescription(Type enumType)
        {
            var nvc = new NameValueCollection();
            var type = typeof(DescriptionAttribute);

            foreach (FieldInfo fi in enumType.GetFields())
            {
                var array = fi.GetCustomAttributes(type, true);
                if (array.Length > 0)
                {
                    nvc.Add(fi.Name, ((DescriptionAttribute)array[0]).Description);
                }
            }

            return nvc;
        }
    }
}
